package org.example.web.controller;

import org.example.domain.Brand;
import org.example.domain.Product;
import org.example.qo.PageResult;
import org.example.qo.ProductQueryObject;
import org.example.service.IBrandService;
import org.example.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author 李新雨的项目86176
 * @version 1.0
 * @date 2024-12-03 9:14
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    //TODO: 2024/12/39:14
    //商品管理
    @Autowired
    private IProductService productService;


    @Autowired
    private IBrandService brandService;


    //http://localhost/product/list
    @RequestMapping("/list")
    public String list(Model model, ProductQueryObject productQueryObject){
        PageResult<Product> productPageResult = productService.selectPage(productQueryObject);
        model.addAttribute("pageResult",productPageResult);
        return "product/list";
    }

    @RequestMapping("/del")
    public String del(Long id){
        productService.deleteByPrimaryKey(id);
        return "redirect:/product/list";
    }


    @RequestMapping("/input")
    public String input(Long id,Model model){
        List<Brand> brands = brandService.selectAll();
        model.addAttribute("brands",brands);
        if(id!=null){
            //注意
            Product product = productService.selectByPrimaryKey(id);
            model.addAttribute("product",product);
        }
        return "product/input";
    }
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Product product){
        if(product.getId()!=null){
            productService.updateByPrimaryKey(product);
        }
        else {
            productService.insert(product);
        }
        return "redirect:/product/list";
    }



}
